//
//  NormalObstacle.m
//  StarBugs
//
//  Created by ?? ? on 1/4/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/NormalObstacle.h"
#include "Util/ImageManager.h"

using namespace cocos2d;

NormalObstacle::~NormalObstacle()
{

}

NormalObstacle::NormalObstacle()
{
    
    rack = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_donut_line");
    addChild(rack);
    mainSprite = new CCSprite();
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    
    donut = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_donut");
    donut->setPosition(ccp(0, 3));
    mainSprite->addChild(donut);
    
    objectType = objectType_normal;
}

void NormalObstacle::setBody(b2World *world)
{
    rack->setPosition(ccp(position.x, position.y + 3) );
//    mainSprite->setPosition(position);
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
	mainBody = world->CreateBody(&bodyDef);
	
    b2CircleShape circ;
    circ.m_radius =10 / PTM_RATIO;
	
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &circ;	
    fixtureDef.restitution = 0.9f;
    
	mainBody->CreateFixture(&fixtureDef);
}

void NormalObstacle::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void NormalObstacle::startBounceAction()
{
    CCScaleTo *scale = CCScaleTo::create(0.1, 0.7);
    CCScaleTo *scale2 = CCScaleTo::create(0.1, 1.2);
    CCScaleTo *scale3 = CCScaleTo::create(0.1, 0.9);
    CCScaleTo *scale4 = CCScaleTo::create(0.1, 1.0);
    
    CCAction *action = CCSequence::create(scale, scale2, scale3, scale4, NULL);
    mainSprite->runAction(action);
}

void NormalObstacle::setMove(cocos2d::CCPoint pt)
{
    if(mainBody != NULL)
    {
        position = pt;
        mainBody->SetTransform(b2Vec2(pt.x/ PTM_RATIO, pt.y/PTM_RATIO), 0);
        rack->setPosition(ccp(pt.x, pt.y + 3));
    }
}